package com.Daisy.swi.client.Data;

public class Reader extends Person{
	private static final long serialVersionUID = -8138264131302233682L;
	protected BookList myBooks=new BookList();
	protected int maxNumOfBooks;
	
	protected String institute;
	/**value can be TYPE_UNDERGRADUATE, TYPE_MASTER, or TYPE_TEACHER */
	protected int type;
	public static int TYPE_UNDERGRADUATE = 1;
	public static int TYPE_MASTER = 2;
	public static int TYPE_TEACHER = 3;
	
	public Reader(String name, String id, String password, String institute, int type){
		setName(name);
		setID(id);
		setPassword(password);
		setInstitute(institute);
		setType(type);
		
		switch (type){
		case 1:
			this.maxNumOfBooks=6;
			break;
		case 2:
		case 3:
			this.maxNumOfBooks=12;
			break;
		default:
			break;
		}
	}
	
	public void setInstitute(String institute) {
		this.institute = institute;
	}
	
	public String getInstitute() {
		return this.institute;
	}
	
	public void setType(int t){
		this.type=t;
	}

	public int getType(){
		return this.type;
	}

	public BookList getBooks(){
		return myBooks;
	}

	public void setBooks(BookList myBooks){
		this.myBooks=myBooks;
	}
	
	public void resetInformation(String name, String id, 
			String password, String institute, int type) {
		setName(name);
		setID(id);
		setPassword(password);
		setInstitute(institute);
		setType(type);
	}

	public BookList search(int way, String str, BookList bookList){
		/**way: 
		 * 1--by name; 
		 * 2--by writer
		 */
		BookList rightBooks=new BookList();
		boolean found = false;
		switch (way){
		case 1:
			found=searchByName(str,bookList,rightBooks);
			break;
		case 2:
			found=searchByWriter(str,bookList,rightBooks);
			break;
		default:
			break;	
		}
		
		if (found){
			return rightBooks;
		}
		else{
			return new BookList();
		}
	}

	public boolean searchByName(String searchName, BookList bookList, BookList rightBooks){
		boolean found=false;

		for (int i=0; i<bookList.size(); i++){
			Book book=bookList.get(i);
			if (searchName.contains(" ")){
				String[] buf=searchName.split(" ");
				boolean b=true;
				for (int j=0; j<buf.length; j++){
					if (!book.getName().toLowerCase().contains(buf[j].toLowerCase())){
						b=false;
						break;
					}
				}
				if (b){
					found=true;
					rightBooks.add(book);
				}
			}
			else if (book.getName().toLowerCase().contains(searchName.toLowerCase())){
				rightBooks.add(book);
				found=true;
			}
		}

		return found;
	}

	public boolean searchByWriter(String searchWriter, BookList bookList, BookList rightBooks){
		boolean found=false;

		for (int i=0; i<bookList.size(); i++){
			Book book=bookList.get(i);
			if (searchWriter.contains(" ")){
				String[] buf=searchWriter.split(" ");
				boolean b=true;
				for (int j=0; j<buf.length; j++){
					if (!book.getWriter().toLowerCase().contains(buf[j].toLowerCase())){
						b=false;
						break;
					}
				}
				if (b){
					found=true;
					rightBooks.add(book);
				}
			}
			else if (book.getWriter().toLowerCase().contains(searchWriter.toLowerCase())){
				rightBooks.add(book);
				found=true;
			}
		}

		return found;
	}
	
	public void borrow(Book book){
		book.lend(this);
		myBooks.add(book);
	}

	public void reBorrow(Book book){
		book.reLend(this);
	}

	public void giveBack(Book book){
		book.beBack();
		myBooks.remove(book);
	}
}
